<template>
	<view class="uploadReportImages">
		<view class="top">
			<view class="title">
				<u--text text="标题" color="#000" size="28rpx" margin="0 18rpx" />
				<view style="border-bottom: 1px solid #efefef;">
					<u--input placeholder="请输入标题（最多30个字，非必填）" maxlength="30" :placeHolderStyle="placeHolderStyle"
						border="bottom" v-model="titleValue"></u--input>
				</view>
			</view>
			<view class="uploadArea">
				<u-upload accept="image" width="170rpx" height="170rpx" :fileList="fileList" @afterRead="afterRead"
					@delete="deletePic" name="file" multiple :uploadText="uploadText" :maxCount="maxCount"></u-upload>
			</view>
		</view>
		<view class="bottom">
			<u-button text="提交" color="#588BF3" shape="circle" @click="submitImages" />
		</view>
	</view>
</template>

<script>
	import {
		reportBaseUrl
	} from '../../config/tendIdConfig'
	export default {
		data() {
			return {
				placeHolderStyle: {
					fontSize: '28rpx',
					color: '#999'
				},
				titleValue: '',
				OtherReportFileTypeEnum: {
					图片: 1,
					文件: 2,
				},
				uploadType: 1, // 1 图片 2文件
				maxCount: 6,
				uploadText: '',
				fileList: []
			}
		},
		onLoad(options) {
			this.uploadType = options.uploadType
			this.maxCount = Number(options.uploadType) === 1 ? 6 : 1
			this.uploadText = Number(options.uploadType) === 1 ? '上传(最多6张)' : '上传文档'
		},
		methods: {
			submitImages() {
				const that = this;
				uni.request({
					url: reportBaseUrl + '/web/other_report/create',
					method: 'POST',
					data: {
						title: that.titleValue,
						fileIds: that.fileList.map(item => item.uploadResult.fileId),
						reportTime: that.$u.timeFormat(new Date(), 'yyyy-mm-dd 00:00:00'),
						userId: that.$store.state.reportInfo.id,
						userCode: that.$store.state.reportInfo.id,
						fileType: that.OtherReportFileTypeEnum.图片
					},
					header: {
						H5accessToken: this.$store.state.reportToken
					},
					success(res) {
						if (res.data.code === 200) {
							that.$u.route({
								type: "redirect",
								url: "/pages_my/other_report/index"
							})
						} else {
							uni.showToast({
								title: res.data.msg,
								icon: "fail"
							})
						}
					},
					fail(error) {
						uni.showToast({
							title: error.data.msg,
							icon: "fail"
						})
					}
				})
			},
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileList.length
				lists.map((item) => {
					this.fileList.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this.fileList[fileListLen]
					this.fileList.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						uploadResult: result,
						url: result.fileUrl
					}))
					fileListLen++
				}
			},
			deletePic(event) {
				this.fileList.splice(event.index, 1)
			},
			async uploadFilePromise(url) {
				const that = this;
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: reportBaseUrl + '/web/fileOss/uploadOtherReport', // 仅为示例，非真实的接口地址
						filePath: url,
						header: {
							H5accessToken: this.$store.state.reportToken
						},
						name: 'file',
						success: (res) => {
							const result = JSON.parse(res.data)
							if (result.code === 200) {
								uni.showToast({
									title: '上传成功',
									icon: 'success'
								})
								setTimeout(() => {
									resolve(result.data)
								}, 1000)
							} else {
								uni.showToast({
									title: '上传失败',
									icon: 'fail'
								})
								setTimeout(() => {
									reject(result.msg)
								}, 1000)
							}
						}
					});
				})
			},
		}
	}
</script>

<style>
	page {
		background-color: #FFF;
	}
</style>

<style lang="scss" scoped>
	.uploadReportImages {
		display: flex;
		flex-direction: column;
		width: 100%;
		padding: 40rpx 0;
		box-sizing: border-box;
		height: 100vh;

		.uploadArea {
			padding: 18rpx;
			box-sizing: border-box;
		}

		.top {
			width: 100%;
			flex: 1;
			display: flex;
			flex-direction: column;
		}

		.bottom {
			width: 100%;
			// min-height: 136rpx;
			padding: 40rpx;
			box-sizing: border-box;
		}
	}
</style>